<template>
  <v-chart class="chart" :option="option" autoresize />
</template>

<script setup lang="ts">
import { use } from 'echarts/core'
import { CanvasRenderer } from 'echarts/renderers'
import { PieChart } from 'echarts/charts'
import {
  TitleComponent,
  TooltipComponent,
  LegendComponent
} from 'echarts/components'
import VChart, { THEME_KEY } from 'vue-echarts'
import { ref, provide, onMounted } from 'vue'

// import type { Echarts } from '@/types/echarts'
import { getSimpleData } from '@/api/echarts'
use([
  CanvasRenderer,
  PieChart,
  TitleComponent,
  TooltipComponent,
  LegendComponent
])

provide(THEME_KEY, 'dark')

// const echartsRef = ref<HTMLDivElement>()
const option = ref({})
// const echartsInfo = ref([] as Echarts[])
const getData = async () => {
  const res = await getSimpleData()
  console.log(res.data.data)
  //   echartsInfo.value = res.data.data
  option.value = {
    title: {
      text: 'xxx 公司的销量图',
      left: 'center'
    },
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b} : {c} ({d}%)'
    },
    legend: {
      orient: 'vertical',
      left: 'left',
      data: res.data.data.map(item => item.name)
    },
    series: [
      {
        name: 'Access From',
        type: 'pie',
        radius: ['40%', '70%'],
        avoidLabelOverlap: false,
        itemStyle: {
          borderRadius: 10
        },
        label: {
          show: false,
          position: 'center'
        },
        emphasis: {
          label: {
            show: true,
            fontSize: 40,
            fontWeight: 'bold'
          }
        },
        labelLine: {
          show: false
        },
        data: res.data.data
      }
    ]
  }
  // 使用刚指定的配置项和数据显示图表。
  //   myChart.setOption(option)
}
onMounted(async () => {
  await getData()
})
</script>

<style scoped>
.chart {
  height: 600px;
}
</style>
